package com.nintendo.frame;

import com.nintendo.entity.pojo.Customer;
import com.nintendo.frame.edit.EditCustomerFrame;
import com.nintendo.mapper.CustomerDao;
import com.nintendo.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import com.nintendo.frame.add.*;
public class CustomerFrame extends JFrame {

    private JTable customerTable;
    private DefaultTableModel tableModel;
    private CustomerDao customerDao;

    public CustomerFrame() {
        setTitle("客户管理");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        // 初始化表格模型
        tableModel = new DefaultTableModel(
                new Object[]{"ID", "姓名", "性别", "公司", "电话", "身份证号"},
                0
        );

        // 创建表格
        customerTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(customerTable);
        add(scrollPane, BorderLayout.CENTER);

        // 初始化 CustomerDao
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        customerDao = sqlSession.getMapper(CustomerDao.class);

        // 加载数据
        loadData();

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton addButton = new JButton("新增");
        JButton editButton = new JButton("修改");
        JButton deleteButton = new JButton("删除");
        JButton queryButton = new JButton("查询");

        buttonPanel.add(addButton);
        buttonPanel.add(editButton);
        buttonPanel.add(deleteButton);
        buttonPanel.add(queryButton);

        add(buttonPanel, BorderLayout.SOUTH);

        // 为按钮添加动作监听器
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addCustomer();
            }
        });

        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                editCustomer();
            }
        });

        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteCustomer();
            }
        });

        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                queryCustomers();
            }
        });

        setVisible(true);
    }

    private void loadData() {
        try (SqlSession session = MyBatisUtils.getSqlSession()) {
            List<Customer> customers = customerDao.selectAll();

            for (Customer customer : customers) {
                tableModel.addRow(new Object[]{
                        customer.getId(),
                        customer.getName(),
                        customer.getSex(),
                        customer.getCompany(),
                        customer.getTel(),
                        customer.getCardid()
                });
            }
        }
    }

    private void addCustomer() {
        // 打开新增客户窗口
        new AddCustomerFrame(this).setVisible(true);
    }

    private void editCustomer() {
        int selectedRow = customerTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要修改的客户", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        int customerId = (int) tableModel.getValueAt(selectedRow, 0);
        Customer customer = customerDao.selectById(customerId);
        if (customer != null) {
            new EditCustomerFrame(this, customer).setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "未找到选中的客户", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void deleteCustomer() {
        int selectedRow = customerTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要删除的客户", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        int customerId = (int) tableModel.getValueAt(selectedRow, 0);
        int confirm = JOptionPane.showConfirmDialog(this, "确定要删除选中的客户吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (confirm == JOptionPane.YES_OPTION) {
            try (SqlSession session = MyBatisUtils.getSqlSession()) {
                customerDao.deleteById(customerId);
                session.commit();
                tableModel.removeRow(selectedRow);
                JOptionPane.showMessageDialog(this, "删除成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "删除失败", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void queryCustomers() {
        // 这里可以添加查询逻辑，例如弹出一个对话框让用户输入查询条件
        // 为了简化，这里假设查询所有客户
        tableModel.setRowCount(0);
        loadData();
    }

    public void refreshTable() {
        tableModel.setRowCount(0);
        loadData();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new CustomerFrame();
        });
    }
}
